package com.ycg.blog.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ycg.blog.common.R;
import com.ycg.blog.constants.CacheConstant;
import com.ycg.blog.pojo.WebInfo;
import com.ycg.blog.pojo.vo.WebInfoVo;
import com.ycg.blog.service.ArticleService;
import com.ycg.blog.service.WebInfoService;
import com.ycg.blog.utils.CacheUtil;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

/**
 * @author YCG
 * @date 2024-03-14 18:54
 * @description 网站信息统计
 */
@Slf4j
@RestController
@RequestMapping("/web")
public class WebInfoController {

    @Resource
    private CacheUtil cacheUtil;


    @Resource
    private WebInfoService webInfoService;

    @Value("${site.create-time}")
    private String createTime;

    @GetMapping("/preview")
    public R<WebInfoVo> preview() {
        // 获取网站信息
        Map<String, Object> map = cacheUtil.getHashMul(CacheConstant.SITE_PREVIEW_KEY, WebInfo.class,"id","serialVersionUID");
        WebInfo preview = BeanUtil.mapToBean(map, WebInfo.class, false, CopyOptions.create());

        WebInfoVo webInfoVo = new WebInfoVo();
        BeanUtil.copyProperties(preview, webInfoVo);
        DateTime now = new DateTime(System.currentTimeMillis());
        // 网站创建日期
        DateTime create = new DateTime(createTime);
        int dateBetween1 = dateBetween(create, now);
        webInfoVo.setWorkTime(dateBetween1);

        DateTime dateTime = new DateTime(webInfoVo.getLastUpdate());
        int updateBetween = dateBetween(dateTime, now);
        webInfoVo.setLastUpdate(String.valueOf(updateBetween));
        return R.success(webInfoVo);
    }

    /**
     * 两天相差天数
     * @param begin
     * @param end
     * @return
     */
    private int dateBetween(DateTime begin, DateTime end) {
        DateBetween between = begin.between(end);
        String day = between.toString().split("天")[0];
        if (day.contains("秒")) {
            return Integer.parseInt("0");
        }
        return Integer.parseInt(day);
    }

    @PostConstruct
    public void initWebInfo() {

        WebInfo webInfo;
        if (webInfoService.count() <= 0) {
            // 说明数据库没有信息 添加一条
            webInfo = new WebInfo();
            webInfo.setCount(6666);
            webInfo.setLastUpdate("2024-04-01 00:00:00");
            webInfo.setMonthViews(0);
            webInfo.setTotalViews(0);
            webInfo.setYesterdayViews(0);
            webInfoService.saveOrUpdate(webInfo);
        } else {
            // 数据库有信息, 取出最后一个
            webInfo = webInfoService.getLast();
            // 将cst日期转染
            DateTime dateTime = DateUtil.parseCST(webInfo.getCreateTime().toString());
            Date formatDate = DateUtil.convertTimeZone(dateTime, TimeZone.getDefault()).toSqlDate();
            webInfo.setCreateTime(formatDate);
        }
        // 读取站点预览信息
        Map<String, Object> map = new HashMap<>();
        BeanUtil.beanToMap(webInfo, map, false,true);
        cacheUtil.setHashMul(CacheConstant.SITE_PREVIEW_KEY, map);
    }
}
